package torres.item;

import java.io.Serializable;

import torres.item.EquippableItem.Type;


/**
 * @author Torres
 * Represents an Entity's equipped items. 
 * Allows items to be added and removed.
 * Calculates the armor mod and offensive mod of all
 * equipped items.
 */
@SuppressWarnings("serial")
public class EquippedInventory implements Serializable
{
	/*
	 * private EquippableItem headItem;
	private EquippableItem leftHandItem;
	private EquippableItem rightHandItem;
	private EquippableItem lowerBodyItem;
	private EquippableItem upperBodyItem;
	*/
	
	private EquippableItem equipArray[] = new EquippableItem[5];
	
	
	/**
	 * Default constructor for the EquippedInventory
	 */
	public EquippedInventory() {
		
		//headItem = leftHandItem = rightHandItem = lowerBodyItem = upperBodyItem = null;
		equipArray = new EquippableItem[5]; 
	}
	
	/** 
	 * Add the specified item to the EquippedInventory
	 * @param The item to be added to the EquippedInventory
	 * @return True if the item was equipped, false otherwise.
	 */
	public boolean addItem(EquippableItem item)
	{
		boolean returnValue = true;
		int index = item.equipType.ordinal();
		if(equipArray[index] == null)
			equipArray[index] = item;
		else
			returnValue = false;
		return returnValue;
	}
	
	/**
	 * Remove the specified item from the inventory, if it exists.
	 * @param item The item to be removed from the inventory.
	 * @return The item removed, if successful. Null otherwise.
	 */
	public EquippableItem removeItem(EquippableItem item)
	{
		EquippableItem returnValue = null;
		int index = item.equipType.ordinal();
		returnValue = equipArray[index];
		equipArray[index] = null;
		
		/*
		switch(item.equipType) {
			case LEFT: returnValue = removeLeftHandItem(item); break;
			case RIGHT: returnValue = removeRightHandItem(item); break;
			case HEAD: returnValue = removeHeadItem(item); break;
			case UPPER: returnValue = removeUpperBodyItem(item); break;
			case LOWER: returnValue = removeLowerBodyItem(item); break;
		}
		*/	
		return returnValue;
	}
	
	/**
	 * Returns the total Armor Modifier of all equipped items.
	 * @return The total Armor Modifier of all equipped items, 0 if nothing is equipped.
	 */
	public int getArmorTotal() {
		int total = 0;
		for(int i = 0; i < equipArray.length; ++i) 
			if(equipArray[i] != null)
				total += equipArray[i].getArmorMod();
		return total;
	}

	/**
	 * Returns the total Weapon Modifier of all equipped items.
	 * @return The total Weapon Modifier of all equipped items, 0 if nothing is equipped.
	 */
	public int getWeaponTotal() {
		int total = 0;
		for(int i = 0; i < equipArray.length; ++i) 
			if(equipArray[i] != null)
				total += equipArray[i].getOffensiveMod();
		return total;
	}
	
	
	/**
	 * Retrieves the item at the specified index.
	 * @param index The index of the item to be fetched.
	 * @return The item at the specified index if it exists, null otherwise.
	 */
	public EquippableItem getItem(int index) {
		return equipArray[index];
	}
	
	
	/**
	 * Returns the array representing the player's equipped inventory.
	 * @return The array representing the player's equipped inventory.
	 */
	public EquippableItem[] getItemArray() {
		return equipArray;
	}
	
	
	/**
	 * Tests if the specified item is equipped.
	 * @param item The item to be tested.
	 * @return True if the item is equipped, false otherwise.
	 */
	public boolean hasItem(TakeableItem item) {

		boolean returnValue = false;
		for(int a = 0; a < equipArray.length; ++a) 
			returnValue = returnValue || equipArray[a].equals(item);
		return returnValue;
	}

	
	/**
	 * Retrieves the item that is currently equipped to the head.
	 * @return The item that is equipped on the Head.
	 */
	public EquippableItem getHeadItem() {
		
		return this.equipArray[Type.HEAD.ordinal()]; 
	}
	
	/**
	 * Retrieves the item that is currently equipped to the left hand.
	 * @return The item that is equipped on the left hand.
	 */
	public EquippableItem getLeftHandItem() {
		
		return this.equipArray[Type.LEFT.ordinal()];
	}
	
	/**
	 * Retrieves the item that is currently equipped to the right hand.
	 * @return The item that is equipped on the right hand.
	 */
	public EquippableItem getRightHandItem() {
		
		return this.equipArray[Type.RIGHT.ordinal()];
	}
	
	/**
	 * Retrieves the item that is currently equipped to the Upper body.
	 * @return The item that is equipped on the Upper body.
	 */
	public EquippableItem getUpperBodyItem() {
		
		return this.equipArray[Type.UPPER.ordinal()];
	}
	
	/**
	 * Retrieves the item that is currently equipped to the Lower body.
	 * @return The item that is equipped on the Lower body.
	 */
	public EquippableItem getLowerBodyItem() {
		
		return this.equipArray[Type.LOWER.ordinal()];
	}
}
